<template>
    <section>
        <el-col :span="12">
            <el-form :model="productItemForm" :rules="productItemrules" ref="productItemForm">
                <el-form-item label="条码/编码" prop="name">
                    <el-input v-model="productItemForm.name"></el-input>
                </el-form-item>
                <el-form-item label="价格" prop="price">
                    <el-input type="number" v-model.number="productItemForm.price"></el-input>
                </el-form-item>
                <el-form-item label="上下架状态" prop="status">
                    <el-select style="display: block" v-model="productItemForm.status" placeholder="请选择活动区域">
                        <el-option label="已上架" value="on"></el-option>
                        <el-option label="已下架" value="off"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="最小可售卖数量" prop="minNum">
                    <el-input type="number" v-model.number="productItemForm.minNum"></el-input>
                </el-form-item>
                <el-form-item label="最大可售卖数量" prop="maxNum">
                    <el-input type="number" v-model.number="productItemForm.maxNum"></el-input>
                </el-form-item>
                <el-form-item label="城市" prop="city">
                    <el-input v-model="productItemForm.city"></el-input>
                </el-form-item>
                <el-form-item label="货主" prop="owners">
                    <el-select style="display: block" v-model="productItemForm.owners" placeholder="请选择">
                        <el-option v-for="item in productItemForm.options" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="营销中心" prop="market">
                    <el-select style="display: block" v-model="productItemForm.market" placeholder="请选择">
                        <el-option v-for="item in productItemForm.options" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="商品描述">
                    <el-input type="textarea"></el-input>
                </el-form-item>
                <el-form-item label="规格描述">
                    <el-input type="textarea"></el-input>
                </el-form-item>
            </el-form>
        </el-col>
        <el-col :span="10" :offset="2">
            <el-form :model="PicEdit">
                <el-form-item>
                    <label>主图</label>
                    <el-upload action="//jsonplaceholder.typicode.com/posts/" :thumbnail-mode="true" type="drag" :on-preview="handlePreview"
                        :on-success="handSuccess">
                        <i class="el-icon-upload"></i>
                        <div class="el-dragger__text"><em>点击上传</em></div>
                        <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
                        </el-upload>
                </el-form-item>
            </el-form>
        </el-col>
        <el-col :span="24" class="toolPage">
            <el-button type="primary" @click="submitForm('productItemForm')">更新库存项</el-button>
            <el-button style="float:right;" @click="returnProductItem">返回</el-button>
        </el-col>
    </section>
</template>
<script>
    export default {
        data() {
            return {
                imgName: '',
                visible: false,
                previewVisible: false,
                previewImageUrl: "",
                PicEdit: {},
                productItemForm: {
                    name: '',
                    price: '',
                    status: '',
                    minNum: '',
                    maxNum: '',
                    city: '',
                    owners: '',
                    market: '',
                    options: [{
                        value: '选项1',
                        label: '黄金糕'
                    }, {
                        value: '选项2',
                        label: '双皮奶'
                    }, {
                        value: '选项3',
                        label: '蚵仔煎'
                    }, {
                        value: '选项4',
                        label: '龙须面'
                    }, {
                        value: '选项5',
                        label: '北京烤鸭'
                    }]
                },
                productItemrules: {
                    name: [
                        { required: true, message: '请输入条码/编码', trigger: 'change blur' }
                    ],
                    price: [
                        { required: true, message: '请输入价格', },
                    ],
                    status: [
                        { required: true, message: '请选择上下架状态', trigger: 'change' }
                    ],
                    minNum: [
                        { required: true, message: '请输入最小可售卖数量', },
                    ],
                    maxNum: [
                        { required: true, message: '请输入最大可售卖数量', },
                    ],
                    city: [
                        { required: true, message: '请输入城市', trigger: 'change blur' }
                    ],
                    owners: [
                        { required: true, message: '请选择货主', trigger: 'change' }
                    ],
                    market: [
                        { required: true, message: '请选择营销中心', trigger: 'change' }
                    ],
                }
            }
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        alert('submit!');
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            handlePreview(file) {
                this.previewImageUrl = file.url;
                this.previewVisible = true;
            },
            handSuccess(response, file, fileList) {
                fileList.length = 0;
                fileList.push(file);
            },
            handleView(name) {
                this.imgName = name;
                this.visible = true;
            },
            returnProductItem() {
                this.$router.push('/products/undefined/product_items');
            }
        }
    }

</script>